﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IrcDotNet;

namespace StreamersBestFriend
{
	class Blacklist
	{
		static List<String> userBlacklist;
		public static List<String> UserBlacklist
		{
			get { return Blacklist.userBlacklist; }
			set { Blacklist.userBlacklist = value; }
		}

		static List<String> songBlacklist;
		public static List<String> SongBlacklist
		{
			get { return songBlacklist; }
			set { songBlacklist = value; }
		}
		IrcUserCollection userlist;

        /// <summary>
        /// constructor, creates and/or loads blacklist files
        /// </summary>
        /// <param name="pUserList"></param>
		public Blacklist(IrcUserCollection pUserList)
		{
			//if file doesnt exist, create it
			if (!File.Exists("userblacklist.txt"))
				File.Create("userblacklist.txt");
			if (!File.Exists("songblacklist.txt"))
				File.Create("songblacklist.txt");

			//read file, write into list
			userBlacklist = File.ReadAllLines("userblacklist.txt").ToList<String>();
			//songBlacklist = File.ReadAllLines("songblacklist.txt").ToList<String>();
            songBlacklist = new List<string>();
			userlist = pUserList;
		}

        /// <summary>
        /// adds a user to userblacklist, if user is in the channel
        /// </summary>
        /// <param name="inputString">the username</param>
        /// <returns>true if added, false if user not on channel</returns>
		public bool addUser(String inputString)
		{
			if (checkForUser(inputString))
			{
				userBlacklist.Add(inputString);
				return true;
			}
			return false;
		}

        /// <summary>
        /// removes a user from the userblacklist, if user is in the channel
        /// </summary>
        /// <param name="inputString">the username</param>
        /// <returns>true if removed, false user is not in the channel</returns>
		public bool removeUser(String inputString)
		{
			if (checkForUser(inputString))
			{
				userBlacklist.Remove(inputString);
				return true;
			}
			return false;
		}

        /// <summary>
        /// checks the userblacklist for a user
        /// </summary>
        /// <param name="inputString">the username</param>
        /// <returns>true if found, false if not</returns>
		public bool checkUser(String inputString)
		{
			foreach (String s in userBlacklist)
			{
				if (inputString.Equals(s)) return true;
			}
			return false;
		}

        /// <summary>
        /// adds a song to the songblacklist
        /// </summary>
        /// <param name="inputString">the youtube url</param>
        /// <returns>true</returns>
		public bool addSong(String inputString)
		{
			songBlacklist.Add(inputString);
			return true;
		}

        /// <summary>
        /// removes a song from the songblacklist
        /// </summary>
        /// <param name="inputString">the youtube url</param>
        /// <returns>true</returns>
		public bool removeSong(String inputString)
		{
			songBlacklist.Remove(inputString);
			return true;
		}

        /// <summary>
        /// checks the songblacklist for the input url
        /// </summary>
        /// <param name="inputString">youtube url</param>
        /// <returns>true if found, otherwise false</returns>
		public bool checkSong(String inputString)
		{
			foreach (String s in songBlacklist)
			{
				if (inputString.Equals(s)) return true;
			}
			return false;
		}

        /// <summary>
        /// checks the userlist for a specific user
        /// </summary>
        /// <param name="inputString">username</param>
        /// <returns>true if found, false if not</returns>
		private bool checkForUser(String inputString)
		{
			foreach (IrcUser iU in userlist)
			{
				if (iU.NickName.Equals(inputString))
				{
					return true;
				}
			}
			return false;
		}

        /// <summary>
        /// writes the blacklists into files
        /// </summary>
		public void writeFiles()
		{
			File.WriteAllLines("userblacklist.txt", userBlacklist);
			File.WriteAllLines("songblacklist.txt", songBlacklist);
		}
	}
}
